﻿using System;
using System.IO; 
using System.Threading;

/*
StreamWrite/Read能做的FileStream都能做
这个只是类库给我们封装专门处理文本的类,
如果用fileStream操作还要进行解码的步骤,并且有可能会将文字分成两半
*/
namespace StreamWriteANDRead
{
    class Program
    {
        static string path = @"D:\My\.Net Project\文件操作\StreamWriteANDRead\Test.txt";
        static void Main(string[] args)
        {
            //StreamRead();
            SreamWrite();
            Console.ReadKey();
        }

        static void StreamRead()
        {
            FileStream stream = File.OpenRead(path);
            //添加读写器,将流中的数据添加给读写器读写
            StreamReader reader = new StreamReader(stream);
            while (true)
            {
                Thread.Sleep(1);
                Console.Write((char)reader.Read());
                if (reader.EndOfStream) break;
            }
            reader.Close();
        }
        static void SreamWrite()
        {
            FileStream stream = File.OpenWrite(path);
            StreamWriter writer = new StreamWriter(stream);
            while (true)
            { 
                string input = Console.ReadLine();
                if (input == "Cancel") break;
                writer.Write(input);
            }
            writer.Close();
        }
    }
}